package com.summer.framework.base.result;

import java.text.MessageFormat;

/**
 * Description: 返回结果工具类
 * @auther Ice Yang
 * Date 2025/7/24 11:02
 */
public class ResultUtil {

    private static final int SUCCESS_CODE = 0;

    private static final String STR_SUCCESS = "执行成功";

    /**
     * 返回成功结果
     * @return 结果
     */
    public static Result<String> success(){
        Result<String> result = new Result<>();
        result.setCode(SUCCESS_CODE);
        result.setMessage(STR_SUCCESS);
        return result;
    }

    /**
     * 返回成功结果
     * @return 结果
     */
    public static <T> Result<T> success(T data){
        Result<T> result = new Result<>();
        result.setCode(SUCCESS_CODE);
        result.setMessage(STR_SUCCESS);
        result.setData(data);
        return result;
    }

    /**
     * 返回异常信息
     * @return 结果
     */
    public static Result<String> error(int code,String message){
        return error(code,message,null);
    }

    /**
     * 返回异常信息
     * @return 结果
     */
    public static Result<String> error(ResultError error){
        return error(error, null);
    }

    /**
     * 返回异常信息
     * @return 结果
     */
    public static Result<String> error(int code,String message,Object... args){
        Result<String> result = new Result<>();
        result.setCode(code);
        String msg = MessageFormat.format(message,args);
        result.setMessage(msg);
        return result;
    }

    /**
     * 返回异常信息
     * @return 结果
     */
    public static Result<String> error(ResultError error,Object... args){
        Result<String> result = new Result<>();
        result.setCode(error.getCode());
        String msg = MessageFormat.format(error.getMessage(),args);
        result.setMessage(msg);
        return result;
    }
}
